﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Disco.Web.Api
{
    public class WebApiApplication : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            CorsConfig.Register(GlobalConfiguration.Configuration);

            // HINT: The order of route definition matters! Hence preserve sequence below.
            ODataConfig.Register(GlobalConfiguration.Configuration);
            WebApiConfig.Register(GlobalConfiguration.Configuration);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception lastErrorWrapper = Server.GetLastError();
            StreamWriter sw = new StreamWriter(Server.MapPath("~/errors.log"), append: true);
            sw.WriteLine("DateTime (UTC): {0:MM-dd-yyyy, HH:mm:ss}; DateTime (local): {1:MM-dd-yyyy, HH:mm:ss}", DateTime.UtcNow, DateTime.Now);
            sw.WriteLine(lastErrorWrapper.ToString());
            sw.WriteLine();
            sw.Close();
        }
    }
}